package com.zaoke.web.service.zone.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.zaoke.web.bo.query.zone.ZoneMod;
import com.zaoke.web.bo.query.zone.ZoneSave;
import com.zaoke.web.bo.zone.Zone;
import com.zaoke.web.dao.zone.ZoneDao;
import com.zaoke.web.service.zone.ZoneService;
import com.zaoke.web.utils.LocalCaches;

@SuppressWarnings("unchecked")
public class ZoneServiceImpl implements ZoneService{
	
	private ZoneDao zoneDao;
	
	private long expireTime = 0;

	public Zone getZoneById(long zoneId,boolean readDb) {
		
		if(!readDb)
		{
			Set<Zone> zones = getAllZones();
			
			if(zones != null && zones.size() > 0)
				for(Zone zone : zones)
					if(zone.getId() == zoneId)
						return zone;
		}
		else
		{
			List<Long> zoneIds = new ArrayList<Long>();
			zoneIds.add(zoneId);
			List<Zone> zones = zoneDao.findZoneByIds(zoneIds);
			
			if(zones!=null && zones.size()>0)
			{
				return zones.get(0);
			}
		}
		return null;
	}

	public Set<Zone> getAllZones(){
		Set<Zone> zones = (Set<Zone>)LocalCaches.get(LocalCaches.ZAOKE_CACHE_ZONES_KEY, System.currentTimeMillis(), expireTime);
		
		if(zones == null){
			zones = zoneDao.getZonesByState(new Integer[]{Zone.STATE_ACTIVE});
			
			if(zones != null && zones.size() > 0)
				LocalCaches.set(LocalCaches.ZAOKE_CACHE_ZONES_KEY, zones, System.currentTimeMillis());
		}
		
		return zones;
	}

	public Set<Zone> getAllZonesInType(int type,boolean readDB)
	{
		Set<Zone> res = new HashSet<Zone>();
		
		if(!readDB)
		{
			Set<Zone> zones = getAllZones();
			if(zones != null && zones.size() > 0)
				for(Zone zone : zones)
				{
					if(zone.getType()==type)
					{
						res.add(zone);
					}
				
				}
		}
		else
		{
			List<Zone> zones = zoneDao.findZonesByType(type, false);
			res.addAll(zones);
		}
		
		return res;
	}
	
	public boolean newZone(ZoneSave input)
	{
		Zone zone = new Zone();
		zone.setName(input.getZonename());
		
		if(input.getType()==Zone.TYPE_PICK)
		{
			zone.setParentId(input.getBus());
			zone.setPickEndTime(input.getPickend());
			zone.setPickStartTime(input.getPickstart());
		}
		if(input.getType()==Zone.TYPE_MERCHANT)
		{
			zone.setParentId(input.getRegion());
		}
		zone.setAddUserId(input.getUserid());
		zone.setType(input.getType());
		
		return zoneDao.newZone(zone);
	}
	
	public long getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(long expireTime) {
		this.expireTime = expireTime;
	}

	public ZoneDao getZoneDao() {
		return zoneDao;
	}

	public void setZoneDao(ZoneDao zoneDao) {
		this.zoneDao = zoneDao;
	}

	@Override
	public boolean updateZone(ZoneMod input) {
		
		if(input!=null)
		{
			Zone zone = new Zone();
			zone.setId(input.getZoneid());
			zone.setName(input.getZonename());
			zone.setLastmodUserId(input.getUserid());
			zone.setState(Zone.STATE_ACTIVE);
			if(input.getType()==Zone.TYPE_PICK)
			{
				zone.setParentId(input.getBus());
				zone.setPickEndTime(input.getPickend());
				zone.setPickStartTime(input.getPickstart());
			}
			if(input.getType()==Zone.TYPE_MERCHANT)
			{
				zone.setParentId(input.getRegion());
			}
			zone.setSequence(input.getSequence());
			return zoneDao.updateZone(zone);
		}
		
		return false;
	}

	@Override
	public boolean deleteZone(Long zoneid) {
		
		if(zoneid!=null)
		{
			List<Long> ids = new ArrayList<Long>();
			ids.add(zoneid);
			List<Zone> zones = zoneDao.findZoneByIds(ids);
			if(zones==null || zones.size()==0)
			{
				return false;
			}
			Zone zone = zones.get(0);
			List<Long> pids = new ArrayList<Long>();
			pids.add(zone.getId());
			List<Zone> pzones = zoneDao.findZoneByParentIds(pids);
			
			List<Long> idsToDel = new ArrayList<Long>();
			
			idsToDel.add(zoneid);
			
			for(Zone z : pzones)
			{
				idsToDel.add(z.getId());
			}
			
			return zoneDao.deleteZone(idsToDel);
		}
		return false;
	}
}
